package orchestration.android.demo.library.model;

import orchestration.android.demo.library.LibraryProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

/**
 * @author  <a href="mailto:car.ferrarini@gmail.com">Carlo Ferrarini</a>
 */
public class Library{

	private static final String TAG="Libary";
	
	private ContentResolver contentResolver;
	private Uri booksUri= LibraryProvider.Books.CONTENT_URI;
	public static final String KEY_TITLE = LibraryProvider.Books.TITLE;
	public static final String KEY_AUTHOR = LibraryProvider.Books.AUTHOR;
	public static final String KEY_ID=LibraryProvider.Books._ID;
	private String[] booksColumnNames={LibraryProvider.Books._ID,LibraryProvider.Books.TITLE,LibraryProvider.Books.AUTHOR,LibraryProvider.Books.LOANED};
	
	/**
	 * @uml.property  name="instance"
	 * @uml.associationEnd  
	 */
	private static Library instance;
	private Library() {
		
	}

	/**
	 * @param cr
	 * @uml.property  name="contentResolver"
	 */
	private void setContentResolver(ContentResolver cr){
		this.contentResolver=cr;
	}
	
	public Cursor getLoanedBooks(Context context,String loaner) {
		Log.i(TAG, "getLoanedBooks");
		setContentResolver(context.getContentResolver());
		String[] selectionArgs={loaner};
		Cursor c =contentResolver.query(booksUri, booksColumnNames, LibraryProvider.Books.LOANED+"==?", selectionArgs,"");
		return c;
	}
	public Cursor getLoanedBooks(Context context) {
		Log.i(TAG, "getLoanedBooks");
		setContentResolver(context.getContentResolver());
		String[] selectionArgs={""};
		Cursor c =contentResolver.query(booksUri, booksColumnNames, LibraryProvider.Books.LOANED+"!=?", selectionArgs,"");
		return c;
	}
	
	public Cursor getNotLoanedBooks(Context context) {
		Log.i(TAG, "geNottLoanedBooks");
		setContentResolver(context.getContentResolver());
		String[] selectionArgs={"0"};
		Cursor c =contentResolver.query(booksUri, booksColumnNames, LibraryProvider.Books.LOANED+"==?", selectionArgs,"");
		return c;
	}

	public Cursor getBooks(Context context) {
		Log.i(TAG, "getBooks");
		setContentResolver(context.getContentResolver());
		return contentResolver.query(booksUri, booksColumnNames, null, null, "");
	}
	
	
	public void returnBook(Context context,long id,String loaner){
		Log.i(TAG, "returnBook");
		setContentResolver(context.getContentResolver());
		Uri book= ContentUris.withAppendedId(booksUri, id);
		ContentValues cv= new ContentValues();
		cv.put(LibraryProvider.Books.LOANED, "");
		contentResolver.update(book, cv, null, null);
	}

	public void makeLoan(Context context,long id, String loaner) {
		Log.i(TAG, "makeLoan id="+ id);
		setContentResolver(context.getContentResolver());
		Uri book= ContentUris.withAppendedId(booksUri, id);
		ContentValues cv= new ContentValues();
		cv.put(LibraryProvider.Books.LOANED, loaner);
		contentResolver.update(book, cv, null, null);
	}

	/**
	 * @return
	 * @uml.property  name="instance"
	 */
	public static Library getInstance(){
		if(instance==null)
			instance= new Library();
		return instance;
	}
}